package com.yzy.soft.system.controller;
import com.yzy.soft.system.dto.MetadataTableDto;
import com.yzy.soft.system.dto.TableDataDto;

import com.yzy.soft.common.persistence.model.MetadataTable;
//import com.yzy.soft.system.dto.MetadataTableDto;
import com.yzy.soft.common.persistence.dao.MetadataTableMapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.common.collect.Lists;

import com.yzy.soft.core.base.controller.BaseController;
import com.yzy.soft.core.base.tips.ErrorTip;
import com.yzy.soft.core.base.tips.Tip;
import com.yzy.soft.system.freemarker.SynchronousMetadata;
import com.yzy.soft.system.service.IMetadataTableService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.Date;
import java.util.List;

/**
* 描述：测试控制层
* @author Yinzhiyuan
* @date 2018-12-18 16:22:22
*/
@Api(value="测试控制层")
@RestController
@RequestMapping("/metadatatable")
public class MetadataTableController extends BaseController{


    private static final Logger logger= LoggerFactory.getLogger(MetadataTableController.class);

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IMetadataTableService metadataTableService;
    
    @Autowired
    private MetadataTableMapper metadataTableMapper;
    @Autowired
    private SynchronousMetadata synchronousMetadata;
    
    
    /**
    * 描述：查询 测试列表
    */
    @ApiOperation(value="查询测试")
    @GetMapping(value = "/page/{id}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public TableDataDto getAll(@RequestParam(name = "page", defaultValue = "1", required = true) int page, @RequestParam(name = "limit", defaultValue = "10", required = true) int pageSize)throws Exception {
    
        Wrapper<MetadataTable> metadataTable=new EntityWrapper<MetadataTable>();
        page=(page-1)*pageSize;
        pageSize=page+pageSize;
        List<MetadataTable> result=metadataTableMapper.selectPage(new RowBounds(page,pageSize),metadataTable);
        TableDataDto tableDataDto=new TableDataDto();
        tableDataDto.setCode("00");
        tableDataDto.setMsg("000");
        tableDataDto.setCount(metadataTableMapper.selectCount(metadataTable));
        tableDataDto.setData(result);
        return tableDataDto;
    }
    
    /**
    * 描述：根据Id 查询 测试
    * @param id  测试id
    */
    @ApiOperation(value="查询测试")
    @GetMapping(value = "/{id}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public MetadataTable findById(@PathVariable("id") Integer id)throws Exception {
        logger.info("MetadataTableController findById: {}",id);
        MetadataTable metadataTable=metadataTableService.selectById(id);
        return metadataTable;
    }

    /**
    * 描述:创建测试
    * @param metadataTableDTO  测试DTO
    */
    @ApiOperation(value="创建测试")
    @PostMapping("")
    public Tip create(@ModelAttribute MetadataTable metadataTable) {
        logger.info("MetadataTableController createMetadataTable: {}",metadataTable);
        metadataTable.setCreatetime(new Date());
        metadataTable.setLastupdatetime(new Date());
        Integer result=metadataTableMapper.insert(metadataTable);
        return SUCCESS_TIP;
    }

   /**
    * 描述：删除测试
    * @param id 测试id
    */
    @ApiOperation(value="删除测试")
    @DeleteMapping("/{id}")
    public Tip delete(@RequestParam(name="ids[]",required = false) Integer[] metadataTable,@PathVariable(name="id",required = false) Integer id) {
        //Collections.<String>
        if(null!=id){
            Integer result=metadataTableService.deleteById(id);
        }else if(metadataTable.length!=0){
            Integer result=metadataTableMapper.deleteBatchIds(Lists.newArrayList(metadataTable) );
        }
        return SUCCESS_TIP;
    }
    @ApiOperation(value="批量删除测试")
    @DeleteMapping("/")
    public Tip delete1(@RequestParam(name="ids[]",required = false) Integer[] metadataTable) {
        if(metadataTable.length!=0){
           Integer result=metadataTableMapper.deleteBatchIds(Lists.newArrayList(metadataTable) );
        }
        return SUCCESS_TIP;
    }


    /**
    * 描述：更新测试
    * @param id 测试id
    */
    @ApiOperation(value="更新测试")
    @PutMapping(value = "" ,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Tip updateMetadataTable(@ModelAttribute MetadataTableDto metadataTableDto) {
//        logger.info("MetadataTableController updateMetadataTable: {}",metadataTable);
        logger.info("MetadataTableController updateMetadataTable: {}",metadataTableDto);
//        Integer result=metadataTableService.updateById(metadataTable);
        return SUCCESS_TIP;
    }

    @GetMapping("/by")
    public TableDataDto getCadtion(@ModelAttribute MetadataTable metadataTable) {
        Wrapper<MetadataTable> wrapper = new EntityWrapper<MetadataTable>();
//        wrapper.where("versionId={0}", metadataTable.getVersionid());
        List<MetadataTable> result = metadataTableMapper.selectList(wrapper);
        TableDataDto tableDataDto = new TableDataDto();
        tableDataDto.setCode("00");
        tableDataDto.setMsg("000");
        tableDataDto.setCount(result.size());
        tableDataDto.setData(result);
        return tableDataDto;
    }

    @PutMapping("/status/{id}")
    public Tip status(@PathVariable("id")Integer id) {

        logger.info("MetadataTableController status 变更使用状态: {}",id);


        Integer result=metadataTableMapper.status(id);
        if(null==result){
           return new ErrorTip(500,"s");
        }
        return SUCCESS_TIP;
    }

    @GetMapping("/metadataTableId/{metadatabaseId}")
    public TableDataDto getbyAccountId(@PathVariable(name="metadatabaseId") Integer metadatabaseId,@RequestParam(name="page",defaultValue = "1",required = true) int page, @RequestParam(name="limit",defaultValue = "10",required = true) int pageSize) {

        TableDataDto tableDataDto=metadataTableService.selectPage(metadatabaseId,page,pageSize);



        return tableDataDto;
    }



    @GetMapping("/dropDown/{metadataDatabaseId}")
    public List<MetadataTable> getbyAccountId(@PathVariable(name="metadataDatabaseId",required = true) Integer metadataTableId ) {
       List<MetadataTable> result=metadataTableMapper.selectDropDownByDatabaseIdAndUserId(metadataTableId);
        return result;
    }
    @ApiOperation("批量将费用归档设置成弃用状态")
    @PostMapping("/batchNuUse")
    public Tip batchNuUse(@RequestParam(name="ids[]",required = false) Integer[] metadataTable) {
        if(metadataTable.length!=0){
           Integer result=metadataTableMapper.batchNuUseIds(Lists.newArrayList(metadataTable) );
        }
        return SUCCESS_TIP;
    }
    @ApiOperation("批量将费用归档设置成可用状态")
    @PostMapping("/batchUse")
    public Tip batchUse(@RequestParam(name="ids[]",required = false) Integer[] metadataTable) {
        if(metadataTable.length!=0){
            Integer result=metadataTableMapper.batchUseIds(Lists.newArrayList(metadataTable) );
        }
        return SUCCESS_TIP;
    }


    @GetMapping("/synchronous/{id}")
    public Tip synchronous(@PathVariable("id") Integer id) {
        try {
            synchronousMetadata.synchronousMetadataTable(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorTip(500,e.getMessage());
        }
//        if(null==result){
//            return new ErrorTip(500,"s");
//        }
        return SUCCESS_TIP;
    }
}